class Role extends Laya.Sprite{
    constructor(){
        super();
    }

    init(type, camp, hp, speed, hitRadius) {
        this.type=type;
        this.camp=camp;
        this.hp=hp;
        this.speed=speed;
        this.hitRadius=hitRadius;

      // 为了实现射击功能，我们给角色类Role增加射击相关属性：
        //射击类型
        this.shootType = 0;//不需要发射子弹
        //射击间隔
        this.shootInterval = 500;
        //下次射击时间
        this.shootTime = Laya.Browser.now() + 2000;
        //当前动作
        this.action;
        //是否是子弹
        this.isBullet = false;




         this.cacheAction();
            //设置机身
         this.body=new Laya.Animation();
         this.addChild(this.body)
        // this.body.play(0,true,this.type+'_fly')
         this.body.on("complete", this, this.onPlayComplete);
         this.bg = '#ff0000'

        this.playAction('fly');//创建后飞行动画
        // thia.onPlayComplete();
    }

//fly down  hit
    playAction(action){
        this.action=action;
      this.body.play(0,true,this.type+'_'+action)
      var bound=this.body.getBounds();//获取自身大小 x,y,widh,height
     //设置机身居中
     this.body.pos(-bound.width/2,-bound.height/2);
            //显示碰撞区域
        // this.graphics.clear();
        this.graphics.drawCircle(0, 0, this.hitRadius, null, "#ff0000");
    
    //  this.pos(240,800);


}

lostHp(lostHp) {
        //减血
        this.hp -= lostHp;
        //根据血量判断是否死亡
        if (this.hp > 0) {
            //如果未死亡，则播放受击动画
            this.playAction("hit");
        } else {
            //播放死亡动画
            this.playAction("down");
        }
    }


 onPlayComplete() {
            //如果死亡动画播放完成
            if(this.action=="down")
            {
                 //停止动画播放
                this.body.stop();
                //碰撞检测中发现visible是fasle，进行移除回收
                this.visible=false;
            }
            else if(this.action=="hit")//如果是受伤动画，下一帧播放飞行动画
            {
                this.playAction("fly");
            }
    }


 //缓存动画
    cacheAction() {
        if (!Role.cached) {
            Role.cached = true;
            //缓存飞机的动作
            Laya.Animation.createFrames(["war/hero_fly1.png", "war/hero_fly2.png"], "hero_fly");
            //缓存集中爆炸动作
            Laya.Animation.createFrames(["war/hero_down1.png", "war/hero_down2.png"
                , "war/hero_down3.png", "war/hero_down4.png"], "hero_down");

            //缓存敌机1飞行动作
            Laya.Animation.createFrames(["war/enemy1_fly1.png"], "enemy1_fly");
            //缓存敌机1爆炸动作
            Laya.Animation.createFrames(["war/enemy1_down1.png", "war/enemy1_down2.png", "war/enemy1_down3.png"
                , "war/enemy1_down4.png"], "enemy1_down");

            //缓存敌机2飞行动作
            Laya.Animation.createFrames(["war/enemy2_fly1.png"], "enemy2_fly");
            //缓存敌机2爆炸动作
            Laya.Animation.createFrames(["war/enemy2_down1.png", "war/enemy2_down2.png", "war/enemy2_down3.png"
                , "war/enemy2_down4.png"], "enemy2_down");
            //缓存敌机2碰撞动作
            Laya.Animation.createFrames(["war/enemy2_hit.png"], "enemy2_hit");

            //缓存敌机3飞行动作
            Laya.Animation.createFrames(["war/enemy3_fly1.png", "war/enemy3_fly2.png"], "enemy3_fly");
            //缓存敌机3爆炸动作
            Laya.Animation.createFrames(["war/enemy3_down1.png", "war/enemy3_down2.png", "war/enemy3_down3.png"
                , "war/enemy3_down4.png", "war/enemy3_down5.png", "war/enemy3_down6.png"], "enemy3_down");
            //缓存敌机3碰撞动作
            Laya.Animation.createFrames(["war/enemy3_hit.png"], "enemy3_hit");

            //缓存子弹动画
            Laya.Animation.createFrames(["war/bullet1.png"], "bullet1_fly");

            //缓存强化包
            Laya.Animation.createFrames(["war/ufo1.png"], "ufo1_fly");
            //缓存医疗包
            Laya.Animation.createFrames(["war/ufo2.png"], "ufo2_fly");
        }
    }


}